package com.stfalcon.chatkit.sample.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.stfalcon.chatkit.sample.R;
import com.stfalcon.chatkit.sample.common.data.ReceiveData;
import com.stfalcon.chatkit.sample.common.data.model.User;
import com.stfalcon.chatkit.sample.features.demo.FrameActivity;
import com.stfalcon.chatkit.sample.holder.AbstractHolder;
import com.stfalcon.chatkit.sample.holder.special.UserSearchHolder;
import com.stfalcon.chatkit.sample.task.AsyncRquest;
import com.stfalcon.chatkit.sample.url.URLConstant;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class UserSearchAdapter extends RecyclerView.Adapter<AbstractHolder> {

    private Context context;

    private List<User> userList;

    public UserSearchAdapter(Context context, List<User> userList)
    {
        this.context = context;

        this.userList = userList;

    }

    @NotNull
    @Override
    public AbstractHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        return new UserSearchHolder(LayoutInflater.from(context).inflate(R.layout.activity_user_layout, parent, false));
    }


    @Override
    public void onBindViewHolder(@NonNull AbstractHolder holder, int position) {

        User userItem = this.userList.get(position);

        holder.getUserName().setText(userItem.getName());

        SharedPreferences sharedPreferences = holder.itemView.getContext().getSharedPreferences("myinfo", Context.MODE_PRIVATE);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AlertDialog.Builder dialog = new AlertDialog.Builder(holder.itemView.getContext());

                dialog.setNegativeButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {


                        StringBuilder builder = new StringBuilder();

                        builder.append("token=")
                                .append(sharedPreferences.getString("token", ""))
                                .append("&friend_id=")
                                .append(userItem.getId());


                        AsyncRquest asyncRquest = new AsyncRquest(URLConstant.ADD_FRIEND, builder.toString());

                        AsyncTask<Object, Object, String>  asyncTask = asyncRquest.execute();

                        try {

                            String res = asyncRquest.get();

                            Gson son = new Gson();

                            ReceiveData<Integer> receiveDatason = son.fromJson(res, ReceiveData.class);

                            Toast.makeText(context, receiveDatason.getMessage(), Toast.LENGTH_LONG).show();
                            if (0 == receiveDatason.getStatus()) {
                                return;
                            }

                            context.startActivity(new Intent(context, FrameActivity.class));



                        } catch (Exception e) {

                        } finally {
                            dialog.cancel();
                        }
                    }
                });

                dialog.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.cancel();
                    }
                });

                dialog.setTitle("添加好友");

                dialog.show();
            }

        });

        holder.getUserHeader().setImageDrawable(ContextCompat.getDrawable(context.getApplicationContext(),R.drawable.applogobig)); /// 这一步必须要做,否则不会显示.
    }

    /**
     * Set dialogs list
     *
     * @param items dialogs list
     */
    public void setItems(List<User> items) {
        this.userList = items;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount()
    {
        Toast.makeText(context, this.userList.size()+": size", Toast.LENGTH_LONG).show();
         return this.userList.size();
    }


}
